package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.domain.UserInfo;
import com.wsd.smartcarwasher.util.StringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsd.smartcarwasher.dao.TChargeRecordMapper;
import com.wsd.smartcarwasher.domain.TChargeRecord;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TChargeRecordService extends ServiceImpl<TChargeRecordMapper, TChargeRecord> {

    @Resource
    UserInfoService userInfoService;

    @Transactional(rollbackFor = Exception.class)
    public void payFinishOrder(TChargeRecord chargeRecord)
    {
        if(chargeRecord.getPaystatus()==1)
        {
            chargeRecord.setPaystatus(2);
            this.updateById(chargeRecord);

            UserInfo userInfo=this.userInfoService.findById(chargeRecord.getUserid());
            if(!StringUtil.isNullOrEmpty(userInfo))
            {
                if(StringUtil.isNullOrEmpty(userInfo.getBalance()))
                {
                    userInfo.setBalance(BigDecimal.ZERO);
                }
                userInfo.setBalance(userInfo.getBalance().add(chargeRecord.getRealPrice()));
                this.userInfoService.updateUser(userInfo);
            }
        }
    }

    /**
     * 查询充值记录
     * @return
     */
    public List<Map<String, Object>> queryChargeRecord(String userId) {
        return this.baseMapper.queryChargeRecord(userId);
    }

    /**
     * 插入记录
     * @param tChargeRecord
     */
    public void insert(TChargeRecord tChargeRecord) {
        this.baseMapper.save(tChargeRecord);
    }

    /**
     * 更新记录
     */
    public void update(TChargeRecord tChargeRecord) {
        this.baseMapper.update(tChargeRecord);
    }

}
